<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/jsp/base.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
    <base href="${__base}"/>
    <link href="styles/liger/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <link href="styles/liger/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
    <script src="styles/liger/jquery/jquery-1.5.2.min.js" type="text/javascript"></script>
    <script src="styles/liger/ligerUI/js/ligerui.all.js" type="text/javascript"></script> 
     <script src="styles/liger/ligerUI/js/plugins/ligerTree.js" type="text/javascript"></script>
    <link href="styles/liger/css/common.css" rel="stylesheet" type="text/css" />  
    <script src="styles/liger/js/common.js" type="text/javascript"></script>   
    <script src="styles/liger/js/LG.js" type="text/javascript"></script>
    <script src="styles/liger/js/ligerui.expand.js" type="text/javascript"></script>  
    <script src="styles/liger/json2.js" type="text/javascript"></script>
    <script src="styles/liger/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script> 
  
    <script src="styles/liger/jquery-validation/jquery.validate.min.js" type="text/javascript"></script> 
    <script src="styles/liger/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
    <script src="styles/liger/jquery-validation/messages_cn.js" type="text/javascript"></script>
    <script src="styles/liger/jquery.form.js" type="text/javascript"></script>

    <script src="styles/liger/js/iconselector.js" type="text/javascript"></script> 
    <script src="styles/liger/js/system/resourcecreate.js" type="text/javascript"></script> 
   <script src="styles/liger/js/system/resourceupdate.js" type="text/javascript"></script> 
    
    
   <link href="styles/liger/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    .l-panel td.l-grid-row-cell-editing { padding-bottom: 2px;padding-top: 2px;}
    </style>
</head>
<body style="padding:2px;height:100%; text-align:center;">
  
  <div id="layout">
    <div position="left" title="主菜单模块" id="mainmenu">
        <ul id="maintree"></ul>
     </div>
    <div position="center" title="子菜单列表"> 
        <form id="mainform">
        <div id="maingrid"  style="margin:2px;"></div> 
        </form>
    </div>
  </div>
  <ul class="iconlist">
  </ul>
  <script type="text/javascript">
        var menu;
        var t;
         var actionNodeID,actionNodeName,actionNodeIcon;
        function onclick11(item, i)
        {
           
            ;
             if(item.text=='增加'){
             
               LG.resourcecreate();
             }
        }
        function onclick112()
        {
            menu.hide();
        }
        function onclick113()
        {
         
           t_delete();
           
        }
         function onclick114()
        {
         
           t_create();
           
        }
        $(function ()
        {
            menu = $.ligerMenu({ top: 100, left: 100, width: 120, items:
            [
            { text: '增加', click: onclick11,icon:'add' },
            { text: '修改', click: onclick114, icon: 'modify' },
            { line: true },
           { text: '删除', click: onclick113, icon: 'delete' },
            { text: '关闭', click: onclick112, icon: 'close' }
            ]
            });
             $("#maintree").bind("contextmenu", function (e)
            {
                menu.show({ top: e.pageY, left: e.pageX });
                return false;
            });
        });
    </script>
  <script type="text/javascript">
    
      //验证
      var maingform = $("#mainform");
      $.metadata.setType("attr", "validate");
      LG.validate(maingform, { debug: true });
      //覆盖本页面grid的loading效果
      LG.overrideGridLoading(); 

     function itemclick(item)
      {
          var editingrow = grid.getEditingRow();
          switch (item.text)
          {
              case "增加":
                  if (editingrow == null)
                  {
                      addNewRow();
                      t=true;
                  } else
                  {
                      LG.tip('请先提交或取消修改');
                  }
                  break;
              case "修改":
                  if (editingrow == null)
                  {
                      beginEdit();
                      t=false;
                  } else
                  {
                      LG.tip('请先提交或取消修改');
                  }
                  break;
              case "保存":
                  if (editingrow != null)
                  {
                      grid.endEdit(editingrow);
                  } else
                  {
                      LG.tip('现在不在编辑状态!');
                  }
                  break;
              case "取消":
                  if (editingrow != null)
                  {
                      grid.cancelEdit(editingrow); 
                  } else
                  {
                      LG.tip('现在不在编辑状态!');
                  }
                  break;
              case "删除": 
                  $.ligerDialog.confirm('确定删除吗?', function (confirm) {
                      if (confirm)
                          f_delete();
                  });
                  break;
              case "操作按钮":
                  var selected = grid.getSelected();
                  if (!selected) return;
                  top.f_addTab(null, selected.resourceName + ' 操作按钮管理', 'sys/resource!btn.shtml?resourceId=' + selected.resourceId);
                  break;
          }
      }

      function f_reload()
      {
          grid.loadData();
      }
        function  t_create()
      {
         
         LG.resourceupdate();
      }
      function t_delete()
      { var data ;
        if(actionNodeID){
      
     
            LG.ajax({
                  url:'sys/resource!delete.shtml',
                  loading:'正在删除中...',
                  data:{ resourceId: actionNodeID,resourceLevel:1 },
                  success: function (data) { 
                      LG.showSuccess('删除成功');
                      f_reload();
                     treeSelect();
                       
                      
                  },
                  error: function (message)
                  {
                      LG.showError(message);
                  }
              });
        }else
	            {
	                  if(!currentResourceId){
	                  LG.tip('没有可以删除的！');
		              return;
	                  }
						
	            }
      }
      function f_delete(t)
      {    var data ;
           
	          var selected = grid.getSelected();
		          if (selected)
		          {
		             
		              if (!selected.resourceId)
		              {
		                  grid.deleteRow(selected);
		                  return;
		              }
		        
		       LG.ajax({
                  url:'sys/resource!delete.shtml',
                  loading:'正在删除中...',
                  data:{ resourceId: selected.resourceId,resourceLevel:2 },
                  success: function (data) { 
                      LG.showSuccess('删除成功');
                      f_reload();
                  },
                  error: function (message)
                  {
                      LG.showError(message);
                  }
              });
		           
	              }
		          else
		          {
		              LG.tip('请选择行!');
		              return;
		          }
	         
             
          }
      
      var toolbarOptions = { 
        items: [ 
            { text: '增加', click: itemclick , img:"styles/liger/icons/silkicons/add.png"}, 
            { line:true },
            { text: '修改', click: itemclick, img: "styles/liger/icons/miniicons/page_edit.gif" },
            { line: true },
            { text: '保存', click: itemclick, img: "styles/liger/icons/silkicons/page_save.png" },
            { line: true },
            { text: '取消', click: itemclick, img: "styles/liger/icons/silkicons/cancel.png" },
            { line: true },
            { text: '删除', click: itemclick, img: "styles/liger/icons/miniicons/page_delete.gif" },
            { line: true },
            { text: '操作按钮', click: itemclick, img: "styles/liger/icons/32X32/document_library.gif" } 
        ]
    };
   function treeSelect()
   { 
    $("#maintree").html('');
   var tree = $("#maintree").ligerTree({
        url: 'sys/resource!json.shtml?' +
    $.param({
        resourceLevel: 1
    }),onContextmenu: function (node, e)
            { 
                actionNodeName= node.data.resourceName;
                actionNodeIcon= node.data.resourceIcon;
                actionNodeID = node.data.resourceId;
                menu.show({ top: e.pageY, left: e.pageX });
                return false;
            },
    
        checkbox: false,
        onClick: function (node)
        {   var editingrow = grid.getEditingRow();
                  
                  if (editingrow != null)
                  {
                      grid.cancelEdit(editingrow); 
                  }
            if (!node.data.resourceId) return;
           
            currentResourceId = node.data.resourceId;
            grid.set('parms', { resourceLevel: 2, attribute5: node.data.resourceId});
            grid.set('url', 'sys/resource!json.shtml?');
     
        }
    }); 
   
   }treeSelect();
   
    var layout = $("#layout").ligerLayout({ leftWidth: 140 });
     
    var grid = $("#maingrid").ligerGrid({
        //headerImg:"styles/liger/icons/silkicons/table.png",title:'表格表头',
        columns: [
                { display: '菜单名', name: 'resourceName', align: 'left', width: 180, minWidth: 60
                , validate: { required: true }
                , editor: { type: 'text' }
                },
                { display: '菜单说明', name: 'resourceDesc', align: 'left', width: 130, minWidth: 60
                , validate: { required: true }
                , editor: { type: 'text' }
                },
                { display: '链接地址', name: 'resourceUri', align: 'left', width: 300, minWidth: 60
                , validate: { required: true }
                , editor: { type: 'text' }
                }
                , { display: '图标', name: 'resourceIcon', align: 'left', width: 230, minWidth: 50
                , editor: { type: 'select',
                    ext:
                    function (rowdata)
                    {
                        return {
                            onBeforeOpen: function ()
                            {
                                
                                currentComboBox = this;
                                f_openIconsWin();
                                return false;
                            },
                            render: function ()
                            {
                                return rowdata.resourceIcon;
                            }
                        };
                    } }
                , render: function (item)
                {
                    return "<div style='width:100%;height:100%;'><img src='" + item.resourceIcon + "' /></div>";
                }
                }
                ], dataAction: 'server', pageSize: 20, toolbar: toolbarOptions, thisresourceId: 'resourceId',
        width: '98%', height: '100%', heightDiff: -5, checkbox: false, usePager: false, enabledEdit: true, clickToEdit: false,
        fixedCellHeight: true, rowHeight: 25
    });


    grid.bind('beforeSubmitEdit', function (e)
    {
        if (!LG.validator.form())
        {
            LG.showInvalid();
            return false;
        }
        return true;
    });
    grid.bind('afterSubmitEdit', function (e)
    {if(!currentResourceId){
   
     LG.tip('保存失败,请选择左边的一级栏目!');
    
     return false;
    } 
        var isAddNew = e.record['__status'] == "add";
       
        
        var data = $.extend({ resourceId: currentResourceId,resourceLevel:2 }, e.newdata);
       
         
            method= t==true ? "create.shtml" : "update.shtml";
           
            if(t==false){
             var selected = grid.getSelected();
		          if (selected)
		          {
		             
		              if (!selected.resourceId)
		              {
		                  grid.deleteRow(selected);
		                   LG.tip('当前数据已经失效！');
		                    grid.loadData();
		                  return;
		              }else{
		               data = $.extend({resourceId:selected.resourceId,resourceLevel:2,resourceType:'menu'},e.newdata);
		              }
		             
		             }
              
              
            }
         var url='sys/resource!'+method;
        
        LG.ajax({
            url:url,
            loading: '正在保存数据中...',
            data: data,
            success: function ()
            {
                grid.loadData();
                LG.tip('保存成功!');
            },
            error: function (message)
            {
                LG.tip(message);
            }
        }); 
    }); 

    function beginEdit()
    {
        var row = grid.getSelectedRow();
        if (!row) { LG.tip('请选择行'); return; }
        grid.beginEdit(row); 
    }
    function addNewRow()
    {
        grid.addEditRow();
    }
  </script>
</body>
</html> 
